<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.css" rel="stylesheet">
  <style>
    #todoList{
      max-width: 800px;
      margin: 50px auto;
      border: 1px solid #333;
      padding: 20px;
      border-radius: 10px;
    }
    .finished {
      background-color: #c4b4b4;
    }

    /* 定义入场的初始状态 */
    .ani1-enter{
      transform: translateX(-100%);
      opacity: 0;
    }
    /* 定义出场最终状态 */
    .ani1-leave-to{
      transform: translatex(100%);
      opacity: 0;
    }
    /* 中间切换状态 使用过渡 */
    .ani1-enter-active, .ani1-leave-active{
      transition: all 500ms;
    }
  </style>
</head>
<body>
  <div id="app">
    <div id="todoList">
      <div class="row">
        <div class="col-md-10">
          <input type="text" v-model.trim="inputTxt" class="form-control">
        </div>
        <div class="col-md-2">
          <button @click="addTodo" class="btn btn-primary">确定</button>
        </div>
      </div>
      <!-- 列表数据 -->
      <ul class="list-group" style="margin-top: 20px;">
        <li class="list-group-item text-center" v-if="todos.length === 0">暂无待办事项</li>
        <transition-group name="ani1">
          <li :class="['list-group-item', {finished:todo.finished }]" :key="index" v-for="(todo,index) in todos">
            <span>{{todo.content}}</span>
            <div class="pull-right">
              <button class="btn btn-danger btn-xs" @click="delItem(index)">删除</button>
              <button class="btn btn-success btn-xs" @click="setTodoFinished(index)" :disabled="todo.finished">{{todo.finished?'已经完成':'点击完成'}}</button>
            </div>
          </li>
        </transition-group>
      </ul>
    </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const vm = new Vue({
      el: '#app',
      data: {
        // input绑定输入新增代办事项内容
        inputTxt: '',
        todos: [
          
        ]
      },
      methods: {
        addTodo(){
          // 当没有输入时 不允许增加
          if(this.inputTxt === ''){
            return false;
          }
          // 添加一个代办事项 
          this.todos.push({
            content: this.inputTxt,
            finished: false
          })
          // 清空输入框
          this.inputTxt = '';
        },
        delItem(index){
          // 删除待办事项
          if(confirm('您确认删除吗')){
            this.todos.splice(index, 1);
          }
        },
        setTodoFinished(index) {
          // 设置待办事项已完成
          this.todos[index].finished = true;
        }
      }
    })
    // arr.splice(index, 1)
  </script>
</body>
</html>